import pandas as pd
from aibox.data.base_time_data_store import BaseTimeDataStore
from aibox.utils.logger import user_system_log


# 过车数据实例类
from aibox_mod_tsce.store.base_mongodb_io import MongoBase
from aibox_mod_tsce.utils.time_util import to_pdtime_ms
from aibox_mod_tsce.utils.tool_util import df_optimize


class TGSPassMongoStore(BaseTimeDataStore):

    def __init__(self, mod_config ,dt_col=None,id_col=None,columns=None,bar_key='pass_car',converter=None):

        if dt_col is None:
            self._dt_col = 'PASSTIME'
            self._id_col = 'TGSID'
            self._columns =  ['PASSTIME', 'CARPLATE', 'PLATETYPE', 'SPEED', 'DRIVEWAY', 'DRIVEDIR', 'CAPTUREDIR', 'TGSID']
        else:
            self._dt_col = dt_col
            self._id_col = id_col
            self._columns = columns
        self._bar_key = bar_key
        self._converter = converter
        self._mongo = MongoBase('passcar',mod_config.mongodb)

        super().__init__( mod_config.es ,self._dt_col,self._id_col, self._columns,self._bar_key,converter=self._converter)


    def get_data(self, startTime, endTime):
        start = int(startTime.tz_localize('Asia/Shanghai').value / 10 ** 6)
        end = int(endTime.tz_localize('Asia/Shanghai').value / 10 ** 6)
        data = self._mongo.collection.find({"PASSTIME": {"$gte": start, "$lt": end}})
        df = pd.DataFrame(list(data))
        if len(df) > 0:
            df['PASSTIME'] = to_pdtime_ms(df['PASSTIME'],False)
            df = self.convert_type(df)
            df = self.data_preocessing(df, start=startTime, end=endTime)
        return df

    def data_preocessing(self, df, start=None, end=None,wupai=True):

        # 删除重复数据，重复数据利用id去识别
        # no_dup_df = df.reset_index().drop_duplicates('id').set_index('id')
        # if len(no_dup_df) < len(df):
        #     user_system_log.warning('过车数据中，按id筛选，发现%r条重复数据。' % (len(df) - len(no_dup_df)))
        # df = no_dup_df

        # 删除无车牌数据
        if wupai:
            no_wupai_df = df[~df.CARPLATE.isin(['无牌', '无车牌000'])]
        if len(no_wupai_df) < len(df):
            user_system_log.warning('过车数据中，发现%r条无牌数据。' % (len(df) - len(no_wupai_df)))

        df = no_wupai_df

        # 删除超出时间范围的数据
        if not (start is None or end is None):
            no_outtime_df = df[(df.PASSTIME >= start) & (df.PASSTIME < end)]

        if len(no_outtime_df) < len(df):
            user_system_log.warning('过车数据中，发现%r条数据超出时间范围[%r,%r]。' % (len(df) - len(no_outtime_df), start, end))
        df = no_outtime_df

        for cols in df.columns:
            if cols not in self._columns:
                del df[cols]

        return df

    def convert_type(self, df):
        df = super().convert_type(df)
        df = df_optimize(df)
        return df
